package top.littlejiang.onlinexam.common.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.littlejiang.onlinexam.common.base.Constant;
import top.littlejiang.onlinexam.common.configuration.TaskConfig;
import top.littlejiang.onlinexam.common.handler.base.BaseExamHandler;
import top.littlejiang.onlinexam.common.utils.ApplicationContextUtils;
import top.littlejiang.onlinexam.common.utils.JSONUtils;
import top.littlejiang.onlinexam.entity.OlexamStuAnswer;
import top.littlejiang.onlinexam.entity.OlexamTask;
import top.littlejiang.onlinexam.mapper.OlexamTaskMapper;
import top.littlejiang.onlinexam.service.OlexamStuAnswerService;
import top.littlejiang.onlinexam.vo.AutoCheckVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @Description
 * @since 2021/4/11 17:27
 */
@Component("auto_check_paper")
@Slf4j
public class AutoCheckHandler implements BaseExamHandler {

    private TaskConfig taskConfig;
    private OlexamTaskMapper taskMapper;
    private OlexamStuAnswerService stuAnswerService;
    private OlexamTask task;

    @Override
    public boolean initTask(OlexamTask olexamTask) {
        task = olexamTask;
        taskMapper = ApplicationContextUtils.getBean(OlexamTaskMapper.class);
        taskConfig = ApplicationContextUtils.getBean(TaskConfig.class);
        stuAnswerService = ApplicationContextUtils.getBean(OlexamStuAnswerService.class);
        return true;
    }


    @Override
    public void run() {
        Map<String, Object> param = JSONUtils.jsonToMap(task.getTaskParams());
        Integer userId = Integer.parseInt((String)param.get("fk_user_id"));
        String examUuid = (String)param.get("fk_exam_uuid");

        List<AutoCheckVO> toChecks = stuAnswerService.selectForAuto(userId, examUuid);
        List<OlexamStuAnswer> checked = new ArrayList<>();

        toChecks.stream().forEach(item->{
            OlexamStuAnswer temp = new OlexamStuAnswer();
            temp.setIsCompress(null);
            temp.setIsDel(null);
            temp.setId(item.getId());
            temp.setStatus(1);
            temp.setGradeId(0);
            // 单选题 判断题
            if (Constant.RADIO_QUESTION.equals(item.getType()) || Constant.TRUE_FALSE_QUESTION.equals(item.getType())){
                if (item.getQuestAnswer().equals(item.getUserAnswer())){
                    temp.setScore(item.getQuestScore());
                }else{
                    temp.setScore(0.0);
                }
                checked.add(temp);
            // 多选题
            }else if(Constant.MULTI_SELECT_QUESTION.equals(item.getType())){
                List<String> answers = JSONUtils.jsonToBean(item.getQuestAnswer(), ArrayList.class);
                List<String> usrAnswers = JSONUtils.jsonToBean(item.getUserAnswer(), ArrayList.class);
                int total = 0;
                for (String one : answers) {
                    if (usrAnswers.indexOf(one) >= 0) {
                        total++;
                    }
                }
                if (total == answers.size()){
                    temp.setScore(item.getQuestScore());
                }else if (total == 0){
                    temp.setScore(0.0);
                }else{
                    temp.setScore(item.getQuestScore() / 2.0);
                }
                checked.add(temp);
            }
        });

        stuAnswerService.saveOrUpdateBatch(checked, checked.size());

        task.setDelay(task.getDelay() + 1);
        taskMapper.updateById(task);

        taskConfig.delete(task.getTaskId());
    }
}
